package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.PProblemTopicBo;
import com.ruoyi.system.domain.vo.PProblemTopicVo;
import com.ruoyi.system.domain.PProblemTopic;
import com.ruoyi.system.mapper.PProblemTopicMapper;
import com.ruoyi.system.service.IPProblemTopicService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 题库Service业务层处理
 *
 * @author ruoyi
 * @date 2022-07-28
 */
@RequiredArgsConstructor
@Service
public class PProblemTopicServiceImpl implements IPProblemTopicService {

    private final PProblemTopicMapper baseMapper;

    /**
     * 查询题库
     */
    @Override
    public PProblemTopicVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询题库列表
     */
    @Override
    public TableDataInfo<PProblemTopicVo> queryPageList(PProblemTopicBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PProblemTopic> lqw = buildQueryWrapper(bo);
        Page<PProblemTopicVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询题库列表
     */
    @Override
    public List<PProblemTopicVo> queryList(PProblemTopicBo bo) {
        LambdaQueryWrapper<PProblemTopic> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PProblemTopic> buildQueryWrapper(PProblemTopicBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PProblemTopic> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTopicId() != null, PProblemTopic::getTopicId, bo.getTopicId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), PProblemTopic::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getA()), PProblemTopic::getA, bo.getA());
        lqw.eq(StringUtils.isNotBlank(bo.getB()), PProblemTopic::getB, bo.getB());
        lqw.eq(StringUtils.isNotBlank(bo.getC()), PProblemTopic::getC, bo.getC());
        lqw.eq(StringUtils.isNotBlank(bo.getD()), PProblemTopic::getD, bo.getD());
        lqw.eq(StringUtils.isNotBlank(bo.getAnswerResult()), PProblemTopic::getAnswerResult, bo.getAnswerResult());
        return lqw;
    }

    /**
     * 新增题库
     */
    @Override
    public Boolean insertByBo(PProblemTopicBo bo) {
        PProblemTopic add = BeanUtil.toBean(bo, PProblemTopic.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改题库
     */
    @Override
    public Boolean updateByBo(PProblemTopicBo bo) {
        PProblemTopic update = BeanUtil.toBean(bo, PProblemTopic.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PProblemTopic entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除题库
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<PProblemTopicVo> queryFontPageList(PProblemTopicBo bo) {
        LambdaQueryWrapper<PProblemTopic> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTopicId() != null, PProblemTopic::getTopicId, bo.getTopicId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), PProblemTopic::getName, bo.getName());
        return baseMapper.selectFontVoPage(lqw);
    }
}
